package com.example.tedtalksimproved.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.tedtalksimproved.R;
import com.example.tedtalksimproved.VideoListFragment;
import com.example.tedtalksimproved.loader.VideoInfo;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * A custom ArrayAdapter used by the {@link AppListFragment} to display the
 * device's installed applications.
 */
public class VideoListAdapter extends ArrayAdapter<VideoInfo> {
  private LayoutInflater mInflater;
  public ImageLoader mImageLoader;
  
  public VideoListAdapter(Context context, int textViewResourceId, List<VideoInfo> objects, ImageLoader imageLoader) {
      super(context, textViewResourceId, objects);
    mImageLoader = imageLoader;
  }



@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null) {
      mInflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      view = mInflater.inflate(R.layout.list_item_icon_text, parent, false);
    }
    
    ViewHolder holder = (ViewHolder) view.getTag(R.id.id_holder);   
    if (holder == null) {
        holder = new ViewHolder(view);
        view.setTag(R.id.id_holder, holder);
    }    
    
    final VideoInfo item = getItem(position);
    if (item.getImage_url() != null) {
        holder.image.setImageUrl(item.getImage_url(), mImageLoader);
    } else {
        holder.image.setImageResource(R.drawable.no_image);
    }
    
	holder.title.setText(item.getTitle());
    holder.author.setText(item.getAuthor());
    holder.category.setText(item.getCategory());
    holder.duration.setText(item.getDuration());
    holder.date.setText(item.getDate());
    holder.favorites.setChecked(item.isFavorite());
    holder.favorites.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(!item.isFavorite()){
            	new VideoListFragment().addToFavorites(item);
                Log.i("CHECKBOX", "CHECKED");
             
            }else{
            	new VideoListFragment().removeFromFavorites(item);
            	Log.i("CHECKBOX", "UNCHECKED");
           
            }          
        }
    });
    
    return view;
  }
  
  private class ViewHolder {
      NetworkImageView image;
      TextView title; 
      TextView author; 
      TextView category; 
      TextView duration; 
      TextView date; 
      final CheckBox favorites;
      
      public ViewHolder(View v) {
          image = (NetworkImageView) v.findViewById(R.id.icon);
          title = (TextView) v.findViewById(R.id.text);
          author = (TextView) v.findViewById(R.id.author);
          category = (TextView) v.findViewById(R.id.category);
          duration = (TextView) v.findViewById(R.id.duration);
          date = (TextView) v.findViewById(R.id.dateAdded);
          favorites = (CheckBox) v.findViewById(R.id.favorites);
         
          v.setTag(this);
      }
  }
}
